package com.ikas.ai.server.module.morningpaper.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.ikas.ai.model.PageQuery;
import com.ikas.ai.server.module.morningpaper.consts.MorningPaperReviewedConst;
import com.ikas.ai.server.module.morningpaper.dao.MorningPaperHisDao;
import com.ikas.ai.server.module.morningpaper.model.dto.MorningPaperTimeTitleDto;
import com.ikas.ai.server.module.morningpaper.model.po.MorningPaperHisPo;
import com.ikas.ai.server.module.morningpaper.util.MorningPaperTimeUtil;
import com.ikas.ai.utils.PageExUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author li.hexian
 * 早报历史记录相关接口及工具
 */
@Service
@Slf4j
public class MorningPaperHisService extends ServiceImpl<MorningPaperHisDao, MorningPaperHisPo> {

    @Autowired
    MorningPaperVariablesService morningPaperVariablesService;

    @Autowired
    MorningPaperOverhaulPlanService morningPaperOverhaulPlanService;

    @Autowired
    MorningPaperTimeUtil morningPaperTimeUtil;

    private static final String STATISTICS_START_TIME_HOURS = "08";
    private static final String STATISTICS_START_TIME_MIN = "00";

    /**
     * 审核
     *
     * @param userName
     */
    public void review(String userName) {
        update(
                new UpdateWrapper<MorningPaperHisPo>()
                        .eq("date", morningPaperTimeUtil.getYesterdayDayDate())
                        .set("reviewer", userName)
                        .set("reviewed", MorningPaperReviewedConst.PAPER_REVIEWED)
        );
    }

    /**
     * 列表
     *
     * @return
     */
    public PageInfo<MorningPaperHisPo> listHis(Date startTime, Date endTime, String updater, String reviewer, PageQuery pageQuery) {
        PageExUtil.configPageHelper(pageQuery);
        List<MorningPaperHisPo> list = list(
                new LambdaQueryWrapper<MorningPaperHisPo>()
                        .ge(startTime != null && endTime != null, MorningPaperHisPo::getDate, startTime)
                        .le(startTime != null && endTime != null, MorningPaperHisPo::getDate, endTime)
                        .eq(StringUtils.isNotBlank(updater), MorningPaperHisPo::getUpdater, updater)
                        .eq(StringUtils.isNotBlank(reviewer), MorningPaperHisPo::getReviewer, reviewer)
                        .eq(StringUtils.isNotBlank(reviewer), MorningPaperHisPo::getReviewed, MorningPaperReviewedConst.PAPER_REVIEWED)
                        .orderByDesc(MorningPaperHisPo::getDate)
        );
        list.forEach(i -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(i.getDate());
            calendar.set(Calendar.HOUR_OF_DAY, morningPaperTimeUtil.getStatisticsStartTimeHours());
            calendar.set(Calendar.MINUTE, morningPaperTimeUtil.getStatisticsStartTimeMin());
            calendar.set(Calendar.SECOND, 0);
            i.setDate(calendar.getTime());
        });
        return new PageInfo<>(list);
    }

    /**
     * 获取详细历史文本
     *
     * @param id
     * @return
     */
    public String getDetail(Long id) {
        return getOne(
                new LambdaQueryWrapper<MorningPaperHisPo>()
                        .eq(MorningPaperHisPo::getId, id)
        ).getDetail();
    }

    /**
     * 根据当天日期更新早报文本
     *
     * @param detail
     * @param userName
     */
    @Transactional(rollbackFor = Exception.class)
    public void refresh(String detail, String userName) {
        Date todayDate = morningPaperTimeUtil.getYesterdayDayDate();
        int count = count(
                new LambdaQueryWrapper<MorningPaperHisPo>().eq(MorningPaperHisPo::getDate, todayDate)
        );
        if (count > 0) {
            update(
                    new UpdateWrapper<MorningPaperHisPo>()
                            .eq("date", todayDate)
                            .set("updater", userName)
                            .set("detail", detail)
            );
        } else {
            MorningPaperHisPo morningPaperHisPo = new MorningPaperHisPo();
            morningPaperHisPo.setUpdater(userName);
            morningPaperHisPo.setCreator(userName);
            morningPaperHisPo.setDate(todayDate);
            morningPaperHisPo.setReviewed(MorningPaperReviewedConst.PAPER_NOT_REVIEWED);
            morningPaperHisPo.setCreateTime(new Date());
            morningPaperHisPo.setDetail(detail);
            save(morningPaperHisPo);
        }
    }

    /**
     * 获取当日早报时间标题
     *
     * @return
     */
    public MorningPaperTimeTitleDto getTodayTimeTitle() {
        MorningPaperTimeTitleDto mto = new MorningPaperTimeTitleDto();
        String time = STATISTICS_START_TIME_HOURS + ":" + STATISTICS_START_TIME_MIN;
        mto.setStartTime(time);
        mto.setEndTime(time);
        mto.setStartDate(morningPaperTimeUtil.getStartDateText());
        mto.setEndDate(morningPaperTimeUtil.getEndDateText());
        return mto;
    }

    /**
     * 是否已经审核
     *
     * @return
     */
    public Boolean hadReviewed() {
        int count = count(
                new LambdaQueryWrapper<MorningPaperHisPo>()
                        .eq(MorningPaperHisPo::getDate, morningPaperTimeUtil.getYesterdayDayDate())
                        .eq(MorningPaperHisPo::getReviewed, MorningPaperReviewedConst.PAPER_REVIEWED)
        );
        return count > 0;
    }
}
